package com.orionlily.redis3t.config;

import lombok.Setter;
import lombok.extern.slf4j.Slf4j;
import org.redisson.Redisson;
import org.redisson.api.RedissonClient;
import org.redisson.config.Config;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * redisson配置
 *
 * https://github.com/redisson/redisson/wiki/8.-%E5%88%86%E5%B8%83%E5%BC%8F%E9%94%81%E5%92%8C%E5%90%8C%E6%AD%A5%E5%99%A8
 *
 * @author li.lc
 * @date 2019-11-08
 */

@Slf4j
@Configuration
@Setter
@ConfigurationProperties("spring.redis")
public class RedissonConfig {

    private String host;

    private Integer port;

    private String password;

    private static final String FMT = "redis://%s:%d";

/**
     * 初始化RedissonClient
     *
     * @author li.lc
     * @date 2019-11-08
     * @return RedissonClient
     */

    @Bean(name = "redissonClient")
    public RedissonClient getRedissonClient(){
        Config config = new Config();
        config.useSingleServer().setAddress(String.format(FMT,host,port));
        config.useSingleServer().setPassword(password);
        log.info("初始化RedissonClient...");
        return Redisson.create(config);
    }
}
